package com.aegis.sdk.signature.model;

import java.util.Map;
import java.util.TreeMap;

/**
 * @Author wuweixin
 *
 * @Version 1.0
 * @Descritube
 */
public class SignatureParams {
    private final String appId;
    private final String nonce;
    /**
     * 预留的，暂时没有用
     */
    private final String token;
    private final String query;
    private final String body;

    private final String timestamp;

    private SignatureParams(Builder builder) {
        this.appId = builder.appId;
        this.nonce = builder.nonce;
        this.token = builder.token;
        this.query = builder.query;
        this.body = builder.body;
        this.timestamp = builder.timestamp;
    }

    public String getAppId() {
        return appId;
    }

    public String getNonce() {
        return nonce;
    }

    public String getToken() {
        return token;
    }

    public String getQuery() {
        return query;
    }

    public String getBody() {
        return body;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public static Builder builder() {
        return new Builder();
    }

    public static class Builder {
        private String appId;
        private String nonce;
        private String token;
        /**
         * 如 name=张三&age=18，这里可以不用整理key的字典顺序，会自动处理
         * 这里填入后，会自动按字典顺序升序排序，即整理后会变成age=18&name=张三
         * query和queryMap参数二选一填入即可
         */
        private String query;
        /**
         * query和queryMap参数二选一填入即可,最后整理后得到的query参数同样会按字典升序
         */
        private Map<String, String> queryMap;
        private String body;
        private String timestamp;

        public Builder timestamp(String timestamp) {
            this.timestamp = timestamp;
            return this;
        }

        public Builder appId(String appId) {
            this.appId = appId;
            return this;
        }

        public Builder nonce(String nonce) {
            this.nonce = nonce;
            return this;
        }

        public Builder token(String token) {
            this.token = token;
            return this;
        }

        public Builder queryMap(Map<String, String> queryMap) {
            this.queryMap = queryMap;
            return this;
        }

        public Builder query(String query) {
            this.query = query;
            return this;
        }

        public Builder body(String body) {
            this.body = body;
            return this;
        }

        public SignatureParams build() {
            sortQuery();
            return new SignatureParams(this);
        }

        public void sortQuery() {
            if (queryMap != null && !queryMap.isEmpty()) {
                // 使用 TreeMap 进行 key 升序排序
                Map<String, String> sortedMap = new TreeMap<>(queryMap);
                StringBuilder sb = new StringBuilder();
                for (Map.Entry<String, String> entry : sortedMap.entrySet()) {
                    if (sb.length() > 0) sb.append("&");
                    sb.append(entry.getKey()).append("=").append(entry.getValue());
                }
                this.query = sb.toString();
                return;
            }
            if (query != null && !query.isBlank()) {
                // 将 query 字符串转为 Map，排序后再拼接
                String[] pairs = query.split("&");
                Map<String, String> tempMap = new TreeMap<>();
                for (String pair : pairs) {
                    String[] kv = pair.split("=", 2);
                    String key = kv[0];
                    String value = kv.length > 1 ? kv[1] : "";
                    tempMap.put(key, value);
                }
                StringBuilder sb = new StringBuilder();
                for (Map.Entry<String, String> entry : tempMap.entrySet()) {
                    if (sb.length() > 0) sb.append("&");
                    sb.append(entry.getKey()).append("=").append(entry.getValue());
                }
                this.query = sb.toString();
            }
        }
    }


}